package com.leber.spring.aop.aspect.test;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class AspectApiAdvisorDemo {
    public static void main(String[] args) {

        //目标缓存
        Map<String, Object> cache = new HashMap<>();

        AspectJProxyFactory proxyFactory = new AspectJProxyFactory(cache);
//        proxyFactory.addAdvice();

        proxyFactory.addAdvisor(new Advisor() {
            @Override
            public Advice getAdvice() {
                return new MethodBeforeAdvice() {
                    @Override
                    public void before(Method method, Object[] args, Object target) throws Throwable {
                        if ("put".equals(method.getName()) && args.length == 2) {
                            System.out.printf("Advisor---缓存存入数据key:%s,value:%s", args[0], args[1]);
                        }
                    }
                };
            }

            @Override
            public boolean isPerInstance() {
                return false;
            }
        });
        Map<String, Object> cacheProxy = proxyFactory.getProxy();
        cacheProxy.put("1", "A");
        System.out.println();
        System.out.println(cache.get("1"));
    }
}
